/**

 */

package main

import (
	"os"
	"fmt"
	"github.com/kongyixueyuan.com/education/sdkInit"
	"github.com/kongyixueyuan.com/education/service"
	"encoding/json"
	"github.com/kongyixueyuan.com/education/web/controller"
	"github.com/kongyixueyuan.com/education/web"
)

const (
	configFile = "config.yaml"
	initialized = false
	EduCC = "educc"
)

func main() {

	initInfo := &sdkInit.InitInfo{

		ChannelID: "kevinkongyixueyuan",
		ChannelConfig: os.Getenv("GOPATH") + "/src/github.com/kongyixueyuan.com/education/fixtures/artifacts/channel.tx",

		OrgAdmin:"Admin",
		OrgName:"Org1",
		OrdererOrgName: "orderer.kevin.kongyixueyuan.com",

		ChaincodeID: EduCC,
		ChaincodeGoPath: os.Getenv("GOPATH"),
		ChaincodePath: "github.com/kongyixueyuan.com/education/chaincode/",
		UserName:"User1",
	}

	sdk, err := sdkInit.SetupSDK(configFile, initialized)
	if err != nil {
		fmt.Printf(err.Error())
		return
	}

	defer sdk.Close()

	err = sdkInit.CreateChannel(sdk, initInfo)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	channelClient, err := sdkInit.InstallAndInstantiateCC(sdk, initInfo)
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println(channelClient)

	//===========================================//

	serviceSetup := service.ServiceSetup{
		ChaincodeID:EduCC,
		Client:channelClient,
	}

	edu := service.Education{
		Name: "双截棍",
		Gender: "周杰伦",
		Nation: "方文山",
		EntityID: "436216789543",
		Place: "台湾",
		BirthDay: "2001年9月20日",
		EnrollDate: "岩烧店的烟味弥漫",
		GraduationDate: "周杰伦",
		SchoolName: "流行乐",
		Major: "50000/年",
		QuaType: "背景音乐：5000/次",
		Length: "直播：直播效益的10%",
		Mode: "商演：1000/次",
		Level: "改编词",
		Graduation: "改编曲",
		CertNo: "周杰伦",
		Photo: "/static/photo/11.png",
	}

	edu2 := service.Education{
		Name: "忘情水",
		Gender: "李安修",
		Nation: "陈耀川 杜自持",
		EntityID: "654676327773",
		Place: "香港",
		BirthDay: "1994年4月30日",
		EnrollDate: "曾经年少爱追梦",
		GraduationDate: "刘德华",
		SchoolName: "流行音乐",
		Major: "20000/年",
		QuaType: "背景音乐：5000/次",
		Length: "直播：直播效益的10%",
		Mode: "商演：1000/次",
		Level: "改编词",
		Graduation: "改编曲",
		CertNo: "刘德华",
		Photo: "/static/photo/22.png",
	}

	msg, err := serviceSetup.SaveEdu(edu)
	if err != nil {
		fmt.Println(err.Error())
	}else {
		fmt.Println("信息发布成功, 交易编号为: " + msg)
	}

	msg, err = serviceSetup.SaveEdu(edu2)
	if err != nil {
		fmt.Println(err.Error())
	}else {
		fmt.Println("信息发布成功, 交易编号为: " + msg)
	}

	// 根据歌手和歌名与名称查询信息
	result, err := serviceSetup.FindEduByCertNoAndName("刘德华","忘情水")
	if err != nil {
		fmt.Println(err.Error())
	} else {
		var edu service.Education
		json.Unmarshal(result, &edu)
		fmt.Println("根据歌手和与歌名查询信息成功：")
		fmt.Println(edu)
	}

	// 根据身份证号码查询信息
	result, err = serviceSetup.FindEduInfoByEntityID("654676327773")
	if err != nil {
		fmt.Println(err.Error())
	} else {
		var edu service.Education
		json.Unmarshal(result, &edu)
		fmt.Println("根据旋律查询信息成功：")
		fmt.Println(edu)
	}

	// 修改/添加信息
	info := service.Education{
		Name: "七里香",
		Gender: "方文山",
		Nation: "钟兴民",
		EntityID: "1234567765432111",
		Place: "台湾",
		BirthDay: "2004年8月3日",
		EnrollDate: "窗外的麻雀 ",
		GraduationDate: "周杰伦",
		SchoolName: "流行乐",
		Major: "50000/年",
		QuaType: "背景音乐：5000/次",
		Length: "直播：直播效益的10%",
		Mode: "商演：1000/次",
		Level: "改词",
		Graduation: "改编曲",
		CertNo: "周杰伦",
		Photo: "/static/photo/11.png",
	}
	msg, err = serviceSetup.ModifyEdu(info)
	if err != nil {
		fmt.Println(err.Error())
	}else {
		fmt.Println("信息操作成功, 交易编号为: " + msg)
	}

	// 根据身份证号码查询信息
	result, err = serviceSetup.FindEduInfoByEntityID("101")
	if err != nil {
		fmt.Println(err.Error())
	} else {
		var edu service.Education
		json.Unmarshal(result, &edu)
		fmt.Println("根据根据歌曲编号查询成功：")
		fmt.Println(edu)
	}

	// 根据证书编号与名称查询信息
	result, err = serviceSetup.FindEduByCertNoAndName("周杰伦","七里香")
	if err != nil {
		fmt.Println(err.Error())
	} else {
		var edu service.Education
		json.Unmarshal(result, &edu)
		fmt.Println("根据查询信息成功：")
		fmt.Println(edu)
	}

	/*
	msg, err = serviceSetup.DelEdu("101")
	if err != nil {
		fmt.Println(err.Error())
	}else {
		fmt.Println("信息删除成功, 交易编号为: " + msg)
	}

	
	result, err = serviceSetup.FindEduInfoByEntityID("101")
	if err != nil {
		fmt.Println(err.Error())
		fmt.Println("根据身份证号码查询信息失败，指定身份证号码的信息不存在或已被删除...")
	} else {
		var edu service.Education
		json.Unmarshal(result, &edu)
		fmt.Println("根据身份证号码查询信息成功：")
		fmt.Println(edu)
	}*/

	//===========================================//

	app := controller.Application{
		Setup: &serviceSetup,
	}
	web.WebStart(app)

}
